package com.llkj.qingnianguoxue.playservice;

import android.app.Activity;
import android.text.TextUtils;

import com.jm.chat.pickerimage.utils.StringUtil;
import com.llkj.qingnianguoxue.util.Md5;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;

import okhttp3.Call;

/**
 * 播放在线音乐
 * Created by wcy on 2016/1/3.
 */
public abstract class PlayOnlineMusic extends PlayMusic {
    private OnlineMusic mOnlineMusic;
    private DownloadFileListener mDownloadListener;
    private RefuseUserMobileNEtListener mRefuseListener;

    public PlayOnlineMusic(Activity activity, OnlineMusic onlineMusic) {
        super(activity, 3);//1表示下载任务数
        mOnlineMusic = onlineMusic;
    }

    public PlayOnlineMusic setDownloadListener(DownloadFileListener listener){
        mDownloadListener = listener;
        return this;
    }

    public PlayOnlineMusic setRefuseMobileNetListener(RefuseUserMobileNEtListener listener){
        mRefuseListener = listener;
        return this;
    }

    @Override
    protected void refuseMobileNet() {
        super.refuseMobileNet();
        if(mRefuseListener!=null){
            mRefuseListener.refuseMobileNet();
        }
    }

    @Override
    protected void getPlayInfo() {
        String artist = "lrc";//mOnlineMusic.getArtist_name();

        if(!TextUtils.isEmpty(mOnlineMusic.getLrclink())){
            String title = Md5.md5(mOnlineMusic.getLrclink());//mOnlineMusic.getTitle();
            // 下载歌词
            String lrcFileName = FileUtils.getLrcFileName(artist, title);
            File lrcFile = new File(FileUtils.getLrcDir() + lrcFileName);
            if (!lrcFile.exists() && !TextUtils.isEmpty(mOnlineMusic.getLrclink())) {
                downloadFile(mOnlineMusic.getLrclink(), FileUtils.getLrcDir(), lrcFileName);
            } else {
                if(mDownloadListener!=null){
                    mDownloadListener.downloadFileListener(0,1,100,lrcFile);
                }
                checkCounter();
            }
        }

        String title = mOnlineMusic.getTitle();
        String musicUrl = mOnlineMusic.getMusicUrl();
        if(!StringUtil.isEmpty(musicUrl)){
            String musicName = musicUrl.substring(musicUrl.lastIndexOf("/"),musicUrl.lastIndexOf("."));
            String musicFileName = FileUtils.getMp3FileName(musicName, title);
            File musicFile = new File(FileUtils.getMusicDir() + musicFileName);

            if (!musicFile.exists() && !TextUtils.isEmpty(mOnlineMusic.getMusicUrl())) {
                downloadMusicUrlFile(mOnlineMusic.getMusicUrl(), FileUtils.getMusicDir(), musicFileName);
            } else {
                if(mDownloadListener!=null){
                    mDownloadListener.downloadFileListener(1,1,100,musicFile);
                }
                checkCounter();
            }
        }

        String musicOrgUrl = mOnlineMusic.getOrgMusicUrl();
        if(!StringUtil.isEmpty(musicOrgUrl)){
            String musicOrgName = musicOrgUrl.substring(musicOrgUrl.lastIndexOf("/"),musicOrgUrl.lastIndexOf("."));
            String musicOrgFileName = FileUtils.getMp3FileName(musicOrgName, title);
            File musicOrgFile = new File(FileUtils.getMusicDir() + musicOrgFileName);

            if (!musicOrgFile.exists() && !TextUtils.isEmpty(mOnlineMusic.getOrgMusicUrl())) {
                downloadOrigMusicUrlFile(mOnlineMusic.getOrgMusicUrl(),FileUtils.getMusicDir(), musicOrgFileName);
            } else {
                if(mDownloadListener!=null){
                    mDownloadListener.downloadFileListener(2,1,100,musicOrgFile);
                }
                checkCounter();
            }
        }

//        music = new Music();
//        music.setType(Music.Type.ONLINE);
//        music.setTitle(title);
//        music.setArtist(artist);
//        music.setAlbum(mOnlineMusic.getAlbum_title());
//        music.setPath(response.getBitrate().getFile_link());
//        music.setDuration(response.getBitrate().getFile_duration() * 1000);
    }

    public void downloadMusicUrlFile(String url, String destFileDir, String destFileName) {
        OkHttpUtils.get().url(url).build()
                .execute(new FileCallBack(destFileDir, destFileName) {
                    @Override
                    public void inProgress(float progress, long total, int id) {
                        if(mDownloadListener!=null){
                            mDownloadListener.downloadFileListener(1,2,progress,null);
                        }
                    }

                    @Override
                    public void onResponse(File file, int id) {
                        if(mDownloadListener!=null){
                            mDownloadListener.downloadFileListener(1,1,100,file);
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if(mDownloadListener!=null){
                            mDownloadListener.downloadFileListener(1,3,0,null);
                        }
                    }

                    @Override
                    public void onAfter(int id) {
                        checkCounter();
                    }
                });
    }

    public void downloadOrigMusicUrlFile(String url, String destFileDir, String destFileName) {
        OkHttpUtils.get().url(url).build()
                .execute(new FileCallBack(destFileDir, destFileName) {
                    @Override
                    public void inProgress(float progress, long total, int id) {
                        if(mDownloadListener!=null){
                            mDownloadListener.downloadFileListener(2,2,progress,null);
                        }
                    }

                    @Override
                    public void onResponse(File file, int id) {
                        if(mDownloadListener!=null){
                            mDownloadListener.downloadFileListener(2,1,100,file);
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if(mDownloadListener!=null){
                            mDownloadListener.downloadFileListener(2,3,0,null);
                        }
                    }

                    @Override
                    public void onAfter(int id) {
                        checkCounter();
                    }
                });
    }

    public void downloadFile(String url, String destFileDir, String destFileName) {
        OkHttpUtils.get().url(url).build()
                .execute(new FileCallBack(destFileDir, destFileName) {
                    @Override
                    public void inProgress(float progress, long total, int id) {
                        if(mDownloadListener!=null){
                            mDownloadListener.downloadFileListener(0,2,progress,null);
                        }
                    }

                    @Override
                    public void onResponse(File file, int id) {
                        if(mDownloadListener!=null){
                            mDownloadListener.downloadFileListener(0,1,100,file);
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if(mDownloadListener!=null){
                            mDownloadListener.downloadFileListener(0,3,0,null);
                        }
                    }

                    @Override
                    public void onAfter(int id) {
                        checkCounter();
                    }
                });
    }

    public interface DownloadFileListener{
        /**
         * @param type
         * 下载类型
         * @param state
         * 下载状态1下载成功，2下载中，3失败
         */
        void downloadFileListener(int type, int state, float progress, File lrcFile);
    }

    public interface RefuseUserMobileNEtListener{
        void refuseMobileNet();
    }
}
